• 方法参考自 The Barra China Equity Model (CNE5)'s 文档

  • 请在环境变量中设置DB_URI指向数据库


In [1]:
%matplotlib inline

import os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import statsmodels.api as sm
from alphamind.api import *
from PyFin.api import *
from alphamind.analysis.crosssetctions import cross_section_analysis

plt.style.use('ggplot')

In [2]:
"""
Back test parameter settings
"""

start_date = '2016-01-01'
end_date = '2018-02-28'
category = 'sw'
level = 1
freq = '20b'
universe = Universe('ashare_ex')

horizon = map_freq(freq)
ref_dates = makeSchedule(start_date, end_date, freq, 'china.sse')

In [3]:
def risk_factor_analysis(factor_name):
    data_source = os.environ['DB_URI']
    engine = SqlEngine(data_source)
    risk_names = list(set(risk_styles).difference({factor_name}))
    industry_names = list(set(industry_styles).difference({factor_name}))
    constraint_risk = risk_names + industry_names
    
    df = pd.DataFrame(columns=['ret', 'ic', 't.'], dtype=float)

    for ref_date in ref_dates:
        df.loc[ref_date, :] = cross_section_analysis(ref_date,
                                                     factor_name,
                                                     universe,
                                                     horizon,
                                                     constraint_risk,
                                                     engine=engine)
    df.index = pd.to_datetime(df.index)
    return df

In [4]:
candidates_factors = ['BETA', 'SIZE']

In [5]:
%%time
res = [risk_factor_analysis(factor) for factor in candidates_factors]


2019-02-10 00:36:31,655 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:31,662 - ALPHA_MIND - INFO - 2016-01-04 00:00:00 is finished with 2772 stocks for BETA
2019-02-10 00:36:31,665 - ALPHA_MIND - INFO - 2016-01-04 00:00:00 risk_exposure: 8.780185359837276e-32
2019-02-10 00:36:34,375 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:34,378 - ALPHA_MIND - INFO - 2016-02-01 00:00:00 is finished with 2773 stocks for BETA
2019-02-10 00:36:34,380 - ALPHA_MIND - INFO - 2016-02-01 00:00:00 risk_exposure: 4.033049288078771e-31
2019-02-10 00:36:37,086 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:37,088 - ALPHA_MIND - INFO - 2016-03-07 00:00:00 is finished with 2774 stocks for BETA
2019-02-10 00:36:37,091 - ALPHA_MIND - INFO - 2016-03-07 00:00:00 risk_exposure: 2.032537819277293e-31
2019-02-10 00:36:39,868 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:39,870 - ALPHA_MIND - INFO - 2016-04-05 00:00:00 is finished with 2802 stocks for BETA
2019-02-10 00:36:39,873 - ALPHA_MIND - INFO - 2016-04-05 00:00:00 risk_exposure: 2.2432609700206877e-31
2019-02-10 00:36:42,305 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:42,308 - ALPHA_MIND - INFO - 2016-05-04 00:00:00 is finished with 2806 stocks for BETA
2019-02-10 00:36:42,310 - ALPHA_MIND - INFO - 2016-05-04 00:00:00 risk_exposure: 1.2048877817558507e-30
2019-02-10 00:36:44,923 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:44,925 - ALPHA_MIND - INFO - 2016-06-01 00:00:00 is finished with 2809 stocks for BETA
2019-02-10 00:36:44,928 - ALPHA_MIND - INFO - 2016-06-01 00:00:00 risk_exposure: 6.215470872434024e-31
2019-02-10 00:36:47,794 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:47,797 - ALPHA_MIND - INFO - 2016-07-01 00:00:00 is finished with 2825 stocks for BETA
2019-02-10 00:36:47,801 - ALPHA_MIND - INFO - 2016-07-01 00:00:00 risk_exposure: 2.6170814493214998e-31
2019-02-10 00:36:50,433 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:50,435 - ALPHA_MIND - INFO - 2016-07-29 00:00:00 is finished with 2836 stocks for BETA
2019-02-10 00:36:50,438 - ALPHA_MIND - INFO - 2016-07-29 00:00:00 risk_exposure: 1.8752182883590546e-31
2019-02-10 00:36:53,039 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:53,041 - ALPHA_MIND - INFO - 2016-08-26 00:00:00 is finished with 2847 stocks for BETA
2019-02-10 00:36:53,044 - ALPHA_MIND - INFO - 2016-08-26 00:00:00 risk_exposure: 5.6561789769353115e-31
2019-02-10 00:36:55,605 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:55,607 - ALPHA_MIND - INFO - 2016-09-27 00:00:00 is finished with 2859 stocks for BETA
2019-02-10 00:36:55,611 - ALPHA_MIND - INFO - 2016-09-27 00:00:00 risk_exposure: 1.299505272639475e-31
2019-02-10 00:36:58,636 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:36:58,638 - ALPHA_MIND - INFO - 2016-11-01 00:00:00 is finished with 2876 stocks for BETA
2019-02-10 00:36:58,640 - ALPHA_MIND - INFO - 2016-11-01 00:00:00 risk_exposure: 5.954053161299235e-31
2019-02-10 00:37:01,328 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:01,330 - ALPHA_MIND - INFO - 2016-11-29 00:00:00 is finished with 2902 stocks for BETA
2019-02-10 00:37:01,335 - ALPHA_MIND - INFO - 2016-11-29 00:00:00 risk_exposure: 2.340972343668577e-31
2019-02-10 00:37:04,010 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:04,012 - ALPHA_MIND - INFO - 2016-12-27 00:00:00 is finished with 2921 stocks for BETA
2019-02-10 00:37:04,014 - ALPHA_MIND - INFO - 2016-12-27 00:00:00 risk_exposure: 1.1077566431397867e-31
2019-02-10 00:37:07,198 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:07,200 - ALPHA_MIND - INFO - 2017-01-25 00:00:00 is finished with 2944 stocks for BETA
2019-02-10 00:37:07,202 - ALPHA_MIND - INFO - 2017-01-25 00:00:00 risk_exposure: 6.061637482612322e-31
2019-02-10 00:37:11,045 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:11,047 - ALPHA_MIND - INFO - 2017-03-01 00:00:00 is finished with 2983 stocks for BETA
2019-02-10 00:37:11,049 - ALPHA_MIND - INFO - 2017-03-01 00:00:00 risk_exposure: 3.2957084145960574e-31
2019-02-10 00:37:14,532 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:14,534 - ALPHA_MIND - INFO - 2017-03-29 00:00:00 is finished with 3024 stocks for BETA
2019-02-10 00:37:14,536 - ALPHA_MIND - INFO - 2017-03-29 00:00:00 risk_exposure: 1.6682637573515395e-31
2019-02-10 00:37:19,301 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:19,304 - ALPHA_MIND - INFO - 2017-04-28 00:00:00 is finished with 3080 stocks for BETA
2019-02-10 00:37:19,308 - ALPHA_MIND - INFO - 2017-04-28 00:00:00 risk_exposure: 9.48598197787303e-32
2019-02-10 00:37:33,470 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:33,473 - ALPHA_MIND - INFO - 2017-05-31 00:00:00 is finished with 3113 stocks for BETA
2019-02-10 00:37:33,475 - ALPHA_MIND - INFO - 2017-05-31 00:00:00 risk_exposure: 8.774776451151662e-32
2019-02-10 00:37:39,335 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:39,337 - ALPHA_MIND - INFO - 2017-06-28 00:00:00 is finished with 3155 stocks for BETA
2019-02-10 00:37:39,339 - ALPHA_MIND - INFO - 2017-06-28 00:00:00 risk_exposure: 1.2561838095457818e-31
2019-02-10 00:37:42,566 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:42,569 - ALPHA_MIND - INFO - 2017-07-26 00:00:00 is finished with 3196 stocks for BETA
2019-02-10 00:37:42,572 - ALPHA_MIND - INFO - 2017-07-26 00:00:00 risk_exposure: 1.6998937536319542e-31
2019-02-10 00:37:46,330 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:46,333 - ALPHA_MIND - INFO - 2017-08-23 00:00:00 is finished with 3231 stocks for BETA
2019-02-10 00:37:46,337 - ALPHA_MIND - INFO - 2017-08-23 00:00:00 risk_exposure: 1.1527077454799027e-31
2019-02-10 00:37:49,467 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:49,469 - ALPHA_MIND - INFO - 2017-09-20 00:00:00 is finished with 3260 stocks for BETA
2019-02-10 00:37:49,472 - ALPHA_MIND - INFO - 2017-09-20 00:00:00 risk_exposure: 4.590807376048834e-31
2019-02-10 00:37:52,887 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:52,889 - ALPHA_MIND - INFO - 2017-10-25 00:00:00 is finished with 3295 stocks for BETA
2019-02-10 00:37:52,891 - ALPHA_MIND - INFO - 2017-10-25 00:00:00 risk_exposure: 2.496839867689659e-31
2019-02-10 00:37:56,328 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:37:56,330 - ALPHA_MIND - INFO - 2017-11-22 00:00:00 is finished with 3332 stocks for BETA
2019-02-10 00:37:56,333 - ALPHA_MIND - INFO - 2017-11-22 00:00:00 risk_exposure: 1.5081910271866655e-31
2019-02-10 00:38:00,992 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:00,995 - ALPHA_MIND - INFO - 2017-12-20 00:00:00 is finished with 3362 stocks for BETA
2019-02-10 00:38:00,997 - ALPHA_MIND - INFO - 2017-12-20 00:00:00 risk_exposure: 4.678452352395579e-31
2019-02-10 00:38:04,434 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:04,436 - ALPHA_MIND - INFO - 2018-01-18 00:00:00 is finished with 3386 stocks for BETA
2019-02-10 00:38:04,439 - ALPHA_MIND - INFO - 2018-01-18 00:00:00 risk_exposure: 5.364881573261643e-32
2019-02-10 00:38:11,308 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:11,311 - ALPHA_MIND - INFO - 2018-02-22 00:00:00 is finished with 3433 stocks for BETA
2019-02-10 00:38:11,313 - ALPHA_MIND - INFO - 2018-02-22 00:00:00 risk_exposure: 3.792625694052645e-06
2019-02-10 00:38:20,225 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:20,228 - ALPHA_MIND - INFO - 2016-01-04 00:00:00 is finished with 2772 stocks for SIZE
2019-02-10 00:38:20,230 - ALPHA_MIND - INFO - 2016-01-04 00:00:00 risk_exposure: 2.4238582429739416e-31
2019-02-10 00:38:27,864 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:27,866 - ALPHA_MIND - INFO - 2016-02-01 00:00:00 is finished with 2773 stocks for SIZE
2019-02-10 00:38:27,869 - ALPHA_MIND - INFO - 2016-02-01 00:00:00 risk_exposure: 5.674415515478732e-31
2019-02-10 00:38:36,504 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:36,507 - ALPHA_MIND - INFO - 2016-03-07 00:00:00 is finished with 2774 stocks for SIZE
2019-02-10 00:38:36,509 - ALPHA_MIND - INFO - 2016-03-07 00:00:00 risk_exposure: 1.0983663637406123e-30
2019-02-10 00:38:42,742 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:42,744 - ALPHA_MIND - INFO - 2016-04-05 00:00:00 is finished with 2802 stocks for SIZE
2019-02-10 00:38:42,747 - ALPHA_MIND - INFO - 2016-04-05 00:00:00 risk_exposure: 4.672276334463717e-31
2019-02-10 00:38:46,184 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:46,186 - ALPHA_MIND - INFO - 2016-05-04 00:00:00 is finished with 2806 stocks for SIZE
2019-02-10 00:38:46,188 - ALPHA_MIND - INFO - 2016-05-04 00:00:00 risk_exposure: 2.5351968948788832e-31
2019-02-10 00:38:49,083 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:49,085 - ALPHA_MIND - INFO - 2016-06-01 00:00:00 is finished with 2809 stocks for SIZE
2019-02-10 00:38:49,087 - ALPHA_MIND - INFO - 2016-06-01 00:00:00 risk_exposure: 1.4284602649215035e-31
2019-02-10 00:38:51,754 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:51,756 - ALPHA_MIND - INFO - 2016-07-01 00:00:00 is finished with 2825 stocks for SIZE
2019-02-10 00:38:51,758 - ALPHA_MIND - INFO - 2016-07-01 00:00:00 risk_exposure: 2.9984250483865363e-31
2019-02-10 00:38:54,796 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:54,798 - ALPHA_MIND - INFO - 2016-07-29 00:00:00 is finished with 2836 stocks for SIZE
2019-02-10 00:38:54,801 - ALPHA_MIND - INFO - 2016-07-29 00:00:00 risk_exposure: 3.379855469188818e-31
2019-02-10 00:38:57,422 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:38:57,425 - ALPHA_MIND - INFO - 2016-08-26 00:00:00 is finished with 2847 stocks for SIZE
2019-02-10 00:38:57,428 - ALPHA_MIND - INFO - 2016-08-26 00:00:00 risk_exposure: 2.7522205190185326e-31
2019-02-10 00:39:00,884 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:00,886 - ALPHA_MIND - INFO - 2016-09-27 00:00:00 is finished with 2859 stocks for SIZE
2019-02-10 00:39:00,889 - ALPHA_MIND - INFO - 2016-09-27 00:00:00 risk_exposure: 2.9462745941606107e-31
2019-02-10 00:39:04,051 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:04,054 - ALPHA_MIND - INFO - 2016-11-01 00:00:00 is finished with 2876 stocks for SIZE
2019-02-10 00:39:04,056 - ALPHA_MIND - INFO - 2016-11-01 00:00:00 risk_exposure: 1.8063646099617438e-31
2019-02-10 00:39:07,254 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:07,256 - ALPHA_MIND - INFO - 2016-11-29 00:00:00 is finished with 2902 stocks for SIZE
2019-02-10 00:39:07,258 - ALPHA_MIND - INFO - 2016-11-29 00:00:00 risk_exposure: 2.34522963155895e-31
2019-02-10 00:39:10,268 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:10,270 - ALPHA_MIND - INFO - 2016-12-27 00:00:00 is finished with 2921 stocks for SIZE
2019-02-10 00:39:10,273 - ALPHA_MIND - INFO - 2016-12-27 00:00:00 risk_exposure: 1.7512207226372508e-31
2019-02-10 00:39:13,599 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:13,601 - ALPHA_MIND - INFO - 2017-01-25 00:00:00 is finished with 2944 stocks for SIZE
2019-02-10 00:39:13,604 - ALPHA_MIND - INFO - 2017-01-25 00:00:00 risk_exposure: 5.009395301391125e-31
2019-02-10 00:39:17,167 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:17,170 - ALPHA_MIND - INFO - 2017-03-01 00:00:00 is finished with 2983 stocks for SIZE
2019-02-10 00:39:17,172 - ALPHA_MIND - INFO - 2017-03-01 00:00:00 risk_exposure: 6.974423870313e-31
2019-02-10 00:39:20,117 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:20,120 - ALPHA_MIND - INFO - 2017-03-29 00:00:00 is finished with 3024 stocks for SIZE
2019-02-10 00:39:20,122 - ALPHA_MIND - INFO - 2017-03-29 00:00:00 risk_exposure: 1.8746122397467916e-31
2019-02-10 00:39:22,817 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:22,819 - ALPHA_MIND - INFO - 2017-04-28 00:00:00 is finished with 3080 stocks for SIZE
2019-02-10 00:39:22,821 - ALPHA_MIND - INFO - 2017-04-28 00:00:00 risk_exposure: 2.8038442856537325e-31
2019-02-10 00:39:25,733 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:25,735 - ALPHA_MIND - INFO - 2017-05-31 00:00:00 is finished with 3113 stocks for SIZE
2019-02-10 00:39:25,738 - ALPHA_MIND - INFO - 2017-05-31 00:00:00 risk_exposure: 9.579696648687605e-31
2019-02-10 00:39:28,943 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:28,945 - ALPHA_MIND - INFO - 2017-06-28 00:00:00 is finished with 3155 stocks for SIZE
2019-02-10 00:39:28,947 - ALPHA_MIND - INFO - 2017-06-28 00:00:00 risk_exposure: 2.145768840762906e-31
2019-02-10 00:39:31,767 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:31,769 - ALPHA_MIND - INFO - 2017-07-26 00:00:00 is finished with 3196 stocks for SIZE
2019-02-10 00:39:31,772 - ALPHA_MIND - INFO - 2017-07-26 00:00:00 risk_exposure: 6.608292029564558e-31
2019-02-10 00:39:34,766 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:34,771 - ALPHA_MIND - INFO - 2017-08-23 00:00:00 is finished with 3231 stocks for SIZE
2019-02-10 00:39:34,774 - ALPHA_MIND - INFO - 2017-08-23 00:00:00 risk_exposure: 6.986067046348979e-31
2019-02-10 00:39:38,125 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:38,127 - ALPHA_MIND - INFO - 2017-09-20 00:00:00 is finished with 3260 stocks for SIZE
2019-02-10 00:39:38,130 - ALPHA_MIND - INFO - 2017-09-20 00:00:00 risk_exposure: 6.498744682276766e-31
2019-02-10 00:39:41,805 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:41,809 - ALPHA_MIND - INFO - 2017-10-25 00:00:00 is finished with 3295 stocks for SIZE
2019-02-10 00:39:41,813 - ALPHA_MIND - INFO - 2017-10-25 00:00:00 risk_exposure: 2.5636667803086236e-31
2019-02-10 00:39:48,430 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:48,433 - ALPHA_MIND - INFO - 2017-11-22 00:00:00 is finished with 3332 stocks for SIZE
2019-02-10 00:39:48,436 - ALPHA_MIND - INFO - 2017-11-22 00:00:00 risk_exposure: 7.884309446523115e-31
2019-02-10 00:39:51,764 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:51,767 - ALPHA_MIND - INFO - 2017-12-20 00:00:00 is finished with 3362 stocks for SIZE
2019-02-10 00:39:51,771 - ALPHA_MIND - INFO - 2017-12-20 00:00:00 risk_exposure: 3.4546698691358624e-31
2019-02-10 00:39:55,211 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:55,214 - ALPHA_MIND - INFO - 2018-01-18 00:00:00 is finished with 3386 stocks for SIZE
2019-02-10 00:39:55,218 - ALPHA_MIND - INFO - 2018-01-18 00:00:00 risk_exposure: 2.2368285841177516e-31
2019-02-10 00:39:58,672 - ALPHA_MIND - WARNING - winsorize_normal normally should not be done after neutralize
2019-02-10 00:39:58,674 - ALPHA_MIND - INFO - 2018-02-22 00:00:00 is finished with 3433 stocks for SIZE
2019-02-10 00:39:58,677 - ALPHA_MIND - INFO - 2018-02-22 00:00:00 risk_exposure: 4.486309577858139e-07
Wall time: 3min 30s

In [6]:
df = pd.DataFrame()

for f_name, data in zip(candidates_factors, res):
    data['factor'] = f_name
    df = df.append(data)

In [7]:
df['abs t.'] = np.abs(df['t.'])
df[['factor', 'abs t.']].groupby('factor').mean().sort_values('abs t.', ascending=False).head()


Out[7]:
abs t.
factor
SIZE 4.764919
BETA 2.538497

In [ ]: